/**
 * Created by charsen on 16/6/1.
 */
directives.directive('addNewSource', ["$rootScope", "tools", "$timeout", function ($rootScope, tools, $timeout) {
    return {
        restrict: 'AE',
        scope: {},
        replace: true,
        link: function (scope, element, attr) {
            $rootScope.$on('ADD_NEW_SOURCE_EV', function (e) {
                $(element).modal('show');
                $(".dropdown").dropdown();
            });
            scope.source = {
                type: 'mysql',
                sourceDesc: '',
                sourceUserName: '',
                dataBaseName: '',
                sourcePassword: '',
                sourceServerIp: '',
                characterEncoding: 'utf8',
                portnumber: '3306',
                maxActive: 100
            };
            var ports = {
                mysql: '3306',
                oracle: '1521',
                sqlServer: '1433',
                postgresql: '5432'
            };
            scope.chooseType = function (type) {
                scope.source.type = type;
                scope.source.portnumber = ports[type];
            };
            scope.chooseEncoding = function (encoding) {
                scope.source.characterEncoding = encoding;
            };




            scope.showAddTable = function(){
                $(element).modal('hide');
                tools.get({
                    method: 'POST',
                    url: '/Ibm-Client/changeDataSource.htm',
                    data:{
                        sourceId:scope.sourceId
                    },
                    succ:function(resp){
                        if(resp.success){

                        }else{
//                            alert(resp.message);
                            $timeout(function(){
                                $rootScope.$broadcast("MESSAGE_EV","数据修改异常，请找管理员联系。");
                                return;
                            },500);
                        }
                    }
                });
                $rootScope.$broadcast('ADD_TABLE_EV',scope.sourceId,scope.sourceName);
                scope.hasDone = false;
            };


            scope.clearAllInput = function(){

                scope.hasDone = false;
                scope.source = {
                    type: 'mysql',
                    sourceDesc: '',
                    sourceUserName: '',
                    dataBaseName: '',
                    sourcePassword: '',
                    sourceServerIp: '',
                    characterEncoding: 'utf8',
                    portnumber: '3306',
                    maxActive: 100
                };
            };

            scope.showThis = function(){

                scope.hasDone = false;
                scope.source = {
                    type: 'mysql',
                    sourceDesc: '',
                    sourceUserName: '',
                    dataBaseName: '',
                    sourcePassword: '',
                    sourceServerIp: '',
                    characterEncoding: 'utf8',
                    portnumber: '3306',
                    maxActive: 100
                };

            };

            scope.hasDone = false;
            scope.sourceId = '';
            scope.sourceName = '';
            scope.msg = '';

            scope.addSource = function (e) {
                if(scope.source.sourceDesc==''|| scope.source.sourceServerIp==''|| scope.source.sourceUserName==''||scope.source.sourcePassword==""){
                    scope.msg = "请填写数据源内容";
                    $timeout(function(){
                        scope.msg = '';
                    },2000);
                    return;
                }

                $(e.target).parent().find("span").text('正在添加...');

                tools.get({
                    url: '/Ibm-Client/addDataSource.htm',
                    data: scope.source,
                    method: 'POST',
                    succ: function (resp) {
                        if (resp.success) {

                            scope.sourceId = resp.value.sourceId;
                            scope.sourceName = resp.value.sourceName;

                            $timeout(function () {
                                $(e.target).parent().find("span").text('添加成功!');

                                $timeout(function(){
                                    scope.hasDone = true;
                                },500);

                                $timeout(function () {
                                    scope.clear();
                                    $(e.target).parent().find("span").text('连接数据源');
                                }, 1000);
                            }, 1500);
                            $rootScope.$broadcast("addSource_Update_EV");
                        }else{

                            $(e.target).parent().find("span").text('连接数据源');
                            scope.msg = resp.message;
                            $timeout(function(){
                                scope.msg = '';
                            },2000);
                           // alert(resp.message);
                        }
                    }
                });
            };
            scope.clear = function () {
                for (var i in scope.source) {
                    scope.source[i] = "";
                }
            }
        },
        templateUrl: "/trend-new-BI/app/components/dbManager/addNewSource/addNewSourceView.html"
    }
}]);